<?php
/***********************************************************************
 * @package     IvanEngine
 * @subpackage  Core
 * @author      Ivan <ivanzx@msn.com>
 * @created     2010-07-17
 ***********************************************************************/
if(defined("IVANENGINE_CORE_ROUTING_ROUTING"))
    die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_ROUTING_ROUTING", true);

require_once (dirname(__FILE__).'/../Common.php');
require_once (dirname(__FILE__).'/Route.php');

abstract class CRouting extends CMap
{
    const OT_Context    = 'OT_Context';
    protected $m_options    = array();
    public function __construct()
    {
        parent::__construct();
        $this->m_options[self::OT_Context] = CRequest::Get()->GetRequestContext();
    }

    public function __destruct()
    {
        unset($this->m_options);
        parent::__destruct();
    }
    
    public function GetOptions()
    {
        return $this->m_options;
    }
    
    public function Initialize($options = array())
    {
        $this->m_options    = $options;
    }
    
    abstract public function GetCurUri();
    
    abstract public function Generate($route, $parameters, $absolute);
    
    protected function _FixUrl($url, $absolute = false)
    {
        if (isset($this->m_options[self::OT_Context][CRequest::RC_Prefix]))
        {
            if (0 === strpos($url, 'http'))
            {
                $url = preg_replace('#https?\://[^/]+#', 
                    '$0'.$this->m_options[self::OT_Context][CRequest::RC_Prefix], $url);
            }
            else
            {
                $url = $this->m_options[self::OT_Context][CRequest::RC_Prefix].$url;
            }
        }

        if ($absolute && isset($this->m_options[self::OT_Context][CRequest::RC_Host]) && 
            0 !== strpos($url, 'http'))
        {
            $url = 'http'.(isset($this->m_options[self::OT_Context][CRequest::RC_IsSecure]) &&
                 $this->m_options[self::OT_Context][CRequest::RC_IsSecure] ? 's' : '').'://'.
                 $this->m_options[self::OT_Context][CRequest::RC_Host].$url;
        }

        return $url;
    }
}
?>
